package language.rmi.complex.server;

import language.rmi.complex.stub.Compute;
import language.rmi.simple.Hello;
import language.rmi.simple.Helloimpl;
import org.junit.Test;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class ComputeEngine {
    public static void main(String[] args) throws RemoteException, InterruptedException {
        System.setProperty("java.security.policy", ComputeEngine.class.getClassLoader().getResource("java.policy").getFile());
        SecurityManager securityManager = new SecurityManager();
        System.setSecurityManager(securityManager);

//            Compute engine = new ComputeEngine();
//            Compute stub = (Compute) UnicastRemoteObject.exportObject(engine, 0);
            Compute compute = new ComputeImpl();
            Registry registry = LocateRegistry.createRegistry(1199);
            registry.rebind("Compute", compute);
            System.out.println("ComputeEngine bound");

            while (true){
                Thread.sleep(100000);
            }
    }

    @Test
    public void Server() throws RemoteException {
        Hello hello = new Helloimpl();
        // 在localhost 1199端口开启了一个RMI服务
        Registry registry = LocateRegistry.createRegistry(1199);
        // 绑定对象到注册表，并给他取名为hello
        registry.rebind("hello", hello);
        System.out.println("hello bound");
    }


}